﻿using System;
using System.Collections.Generic;
using System.Linq;
using OrandoLabs.EnduroStore;

namespace EnduroUserDataSample
{
    [EnduroClass("user")]
    public class UserObject : EnduroObject
    {
        public UserObject()
            : base()
        {
        }
        [EnduroProperty("email")]
        public string Email
        {
            get
            {
                return ToString("email");
            }
            set
            {
                SetValue("email", value);
            }
        }
        [EnduroProperty("prefs")]
        public PrefsObject Prefs
        {
            get
            {
                return ToObject<PrefsObject>("prefs");
            }
            set
            {
                SetObjectValue("prefs", value);
            }
        }
        [EnduroProperty("links")]
        public LinkObject[] Links
        {
            get
            {
                return ToList<LinkObject>("links").ToArray();
            }
        }

        public void AddLink(LinkObject o)
        {
            AddObjectValue("links", o);
        }

        public void RemoveLink(LinkObject o)
        {
            RemoveObjectValue("links", o);
        }
    }

    [EnduroClass("prefs")]
    public class PrefsObject : EnduroObject
    {
        public PrefsObject()
        {
        }

        [EnduroProperty("age")]
        public int Age
        {
            get
            {
                return ToInt("age");
            }
            set
            {
                SetValue<int>("age", value);
            }
        }
        [EnduroProperty("language")]
        public String Language
        {
            get
            {
                return ToString("language");
            }
            set
            {
                SetValue<string>("language", value);
            }
        }
        [EnduroProperty("birthday")]
        public DateTime Birthday
        {
            get
            {
                return ToDateTime("birthday");
            }
            set
            {
                SetValue("birthday", value);
            }
        }
    }

    [EnduroClass("link")]
    public class LinkObject : EnduroObject, IComparable
    {
        public LinkObject()
        {
        }

        [EnduroProperty("url")]
        public String Url
        {
            get
            {
                return ToString("url");
            }
            set
            {
                SetValue<string>("url", value);
            }
        }
        [EnduroProperty("title")]
        public String Title
        {
            get
            {
                return ToString("title");
            }
            set
            {
                SetValue<string>("title", value);
            }
        }

        public int CompareTo(object obj)
        {
            LinkObject that = obj as LinkObject;
            return this.Title.CompareTo(that.Title);
        }
    }
}
